<!--
 * @Author: Vi卡农
 * @Date: 2020-03-03 14:16:44
 * @LastEditTime: 2020-05-07 14:46:33
 * @Description: -商机中心-客源资源导入-
 * @FilePath: \cshf_admin\src\views\modules\resource\resourcecustomerimport.vue
 -->
<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-resource__resourcecustomerimport}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="dataForm.id" placeholder="id" clearable style="width:100px"></el-input>
        </el-form-item>
        <el-form-item>
          客户姓名：
          <el-input
            v-model="dataForm.customerName"
            placeholder="客户姓名"
            style="width:120px"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          客户电话：
          <el-input
            v-model="dataForm.customerPhone1"
            placeholder="客户电话"
            style="width:120px"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          跟进日期：
          <el-date-picker
            v-model="dataForm.followTimes"
            type="date"
            placeholder="开始时间"
            value-format="yyyy-MM-dd"
            style="width:136px"
          ></el-date-picker>
          {{' -'}}
          <el-date-picker
            v-model="dataForm.followTimee"
            type="date"
            placeholder="结束时间"
            value-format="yyyy-MM-dd"
            style="width:136px"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            v-if="$hasPermission('resource:resourcecustomerimport:input')"
            type="primary"
            @click="ImportHandle()"
          >导入</el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            v-if="$hasPermission('resource:resourcecustomerimport:outUser')"
            type="primary"
            @click="distribution()"
          >分配</el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            v-if="$hasPermission('resource:resourcecustomerimport:delete')"
            type="danger"
            @click="deleteHandle()"
          >{{ $t('deleteBatch') }}</el-button>
        </el-form-item>

        <!-- <el-form-item>
          <el-button
            v-if="$hasPermission('resource:resourcecustomerimport:save')"
            type="primary"
            @click="addOrUpdateHandle()"
          >{{ $t('add') }}</el-button>
        </el-form-item>-->
      </el-form>
      <el-table
        v-loading="dataListLoading"
        :data="dataList"
        border
        @selection-change="dataListSelectionChangeHandle"
        @row-click="getPageFollow"
        style="width: 100%;"
      >
      <el-table-column type="expand">
          <template slot-scope="props">
            <div class="houseinfoFollow">
              <el-card class="box-card FY_details" shadow="hover">
                <el-row>
                  <el-col :span="24">
                    <el-table :data="FollowTableData" class="FY_table_gj" max-height="150">
                      <el-table-column prop="content" label="跟进内容"></el-table-column>
                      <el-table-column prop="creatorName" label="跟进人" width="120"></el-table-column>
                      <el-table-column prop="deptName" label="跟进部门" width="120"></el-table-column>
                      <el-table-column prop="createDate" label="跟进时间" width="150"></el-table-column>
                    </el-table>
                  </el-col>
                </el-row>
              </el-card>
            </div>
          </template>
        </el-table-column>
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <!-- <el-table-column prop="id" label="主键id" header-align="center" align="center"></el-table-column> -->
        <el-table-column prop="customerName" label="客户姓名" header-align="center" align="center"></el-table-column>
        <el-table-column prop="customerPhone1" label="客户电话1" header-align="center" align="center"></el-table-column>
        <el-table-column prop="company" label="公司信息" header-align="center" align="center"></el-table-column>
        <!-- <el-table-column prop="customerPhone2" label="客户电话1" header-align="center" align="center"></el-table-column> -->
        <!-- <el-table-column prop="outUser" label="转出人" header-align="center" align="center"></el-table-column> -->
        <!-- <el-table-column prop="tenantCode" label="城市租户" header-align="center" align="center"></el-table-column> -->
        <!-- <el-table-column prop="deleteFlag" label="删除状态" header-align="center" align="center"></el-table-column> -->
        <!-- <el-table-column prop="creator" label="创建人" header-align="center" align="center"></el-table-column> -->
        <!-- <el-table-column prop="deptId" label="创建人部门" header-align="center" align="center"></el-table-column> -->
        <el-table-column prop="createDate" label="创建时间" header-align="center" align="center"></el-table-column>
        <el-table-column prop="followTime" label="跟进时间" header-align="center" align="center"></el-table-column>
        <!-- <el-table-column prop="updater" label="修改人" header-align="center" align="center"></el-table-column> -->
        <!-- <el-table-column prop="updateDate" label="修改时间" header-align="center" align="center"></el-table-column> -->
        <el-table-column
          :label="$t('handle')"
          fixed="right"
          header-align="center"
          align="center"
          width="150"
        >
          <template slot-scope="scope">
            <el-button
              v-if="$hasPermission('resource:resourcecustomerimport:update')"
              type="text"
              size="small"
              @click="addOrUpdateHandle(scope.row.id)"
            >{{ $t('update') }}</el-button>
            <el-button
              v-if="$hasPermission('resource:resourcecustomerimport:delete')"
              type="text"
              size="small"
              @click="deleteHandle(scope.row.id)"
            >{{ $t('delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
        <div class="paginationBox">
        <el-pagination
          :current-page="page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="limit"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="pageSizeChangeHandle"
          @current-change="pageCurrentChangeHandle"
        ></el-pagination>
      </div>
      <!-- <div class="ZWbox2"></div>
      <div class="paginationBox2" :style="'width:'+autoWidthVal">
        <el-card class="box-card martop15 FY_details" shadow="hover">
          <el-table :data="FollowTableData" class="FY_table_gj" height="175">
            <el-table-column prop="content" label="跟进内容"></el-table-column>
            <el-table-column prop="creatorName" label="跟进人" width="120"></el-table-column>
            <el-table-column prop="deptName" label="跟进部门" width="120"></el-table-column>
            <el-table-column prop="createDate" label="跟进时间" width="150"></el-table-column>
          </el-table>
        </el-card>
        <el-pagination
          :current-page="page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="limit"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="pageSizeChangeHandle"
          @current-change="pageCurrentChangeHandle"
        ></el-pagination>
      </div> -->
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
      <!-- 弹窗, 上传文件 -->
      <el-dialog :visible.sync="uploadVisible" title="导入数据" :destroy-on-close="true" width="403px">
        <el-upload
          class="upload-demo"
          drag
          action="/resource/resourcehouseimport/input"
          :multiple="false"
          :before-upload="uploadExcel"
        >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">
            将文件拖到此处，或
            <em>点击上传</em>
          </div>
          <div class="el-upload__tip" slot="tip">只能上传excel表格文件，且不超过500kb</div>
        </el-upload>
      </el-dialog>
      <!-- 分配弹窗 -->
      <el-dialog :visible.sync="outUserVisible" title="分配" :destroy-on-close="true" width="353px">
        <el-form :inline="true" :model="outUser" label-width="80px">
          <el-form-item label="部门">
            <el-cascader
              class="w100"
              v-model="outUser.deptSelectVal"
              :options="this.$store.state.DeptList"
              :show-all-levels="false"
              @change="deptIdChange"
              :props="deptProps"
              ref="myCascader"
              clearable
            ></el-cascader>
          </el-form-item>
          <el-form-item label="人员">
            <el-select v-model="outUser.outUser" placeholder="人员" clearable class="w100">
              <el-option
                v-for="item in maintainList"
                :key="item.id"
                :label="item.realName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <template slot="footer">
          <el-button @click="outUserVisible = false">{{ $t('cancel') }}</el-button>
          <el-button type="primary" @click="distributionFun()">{{ $t('confirm') }}</el-button>
        </template>
      </el-dialog>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from "@/mixins/view-module";
import AddOrUpdate from "./resourcecustomerimport-add-or-update";
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: "/resource/resourcecustomerimport/page",
        getDataListIsPage: true,
        exportURL: "/resource/resourcecustomerimport/export",
        deleteURL: "/resource/resourcecustomerimport",
        deleteIsBatch: true
      },
      dataForm: {
        id: "",
        customerName: "",
        customerPhone1: "",
        followTimes: "",
        followTimee: ""
      },
      autoWidthVal: "",
      FollowTableData: [],
      uploadVisible: false,
      outUser: {
        deptSelectVal: "",
        outUser: ""
      },
      deptProps: {
        value: "id",
        label: "name",
        checkStrictly: true
      },
      outUserVisible: false,
      maintainList: this.$store.state.maintainList
    };
  },
  components: {
    AddOrUpdate
  },
  created() {
    this.autoWidth();
  },
  mounted() {
    let _this = this;
    window.onresize = function() {
      _this.autoWidth();
    };
  },
  methods: {
    autoWidth() {
      let w = document.documentElement.scrollWidth || document.body.scrollWidth;
      this.autoWidthVal = w - 270 + "px";
    },
    //点击导入
    ImportHandle() {
      this.uploadVisible = true;
    },
    //导入表格事件
    uploadExcel(file) {
      let _this = this;
      let formData = new FormData();
      formData.append("file", file);
      this.$http
        .post("/resource/resourcecustomerimport/input", formData, {
          params: {}
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message.success("上传成功！");
          _this.uploadVisible = false;
        })
        .catch(() => {});
      return false;
    },
    //获取客源跟进记录
    getPageFollow(row) {
      let _this = this;
      this.$http
        .get(`/resource/resourcecustomerimport/pageFollow`, {
          params: {
            page: 1,
            limit: 50,
            impId: row.id
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          _this.FollowTableData = res.data.list;
        })
        .catch(() => {});
    },
    //资源分配
    distribution() {
      if (this.dataListSelections.length > 0) {
        this.outUserVisible = true;
      } else {
        this.$message.error("请选择要分配的资源");
      }
    },
    deptIdChange(val) {
      let a = "";
      if (this.$refs.myCascader.getCheckedNodes()[0]) {
        a = this.$refs.myCascader.getCheckedNodes()[0].value;
        this.getUserList(a);
      }
    },
    // 获取部门员工列表
    getUserList(deptId) {
      let _this = this;
      this.$http
        .get(`/sys/user/deptUserList`, {
          params: {
            deptId: deptId
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          _this.maintainList = res.data;
        })
        .catch(() => {});
    },
    //确定分配
    distributionFun() {
      let _this = this;
      let arr = [];
      for (let i = 0; i < _this.dataListSelections.length; i++) {
        const element = _this.dataListSelections[i];
        arr.push(element.id);
      }
      this.$http
        .post(
          "/resource/resourcecustomerimport/outUser",
          {},
          {
            params: {
              ids: arr.join(),
              outUser: _this.outUser.outUser
            }
          }
        )
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message.success("分配成功！");
          _this.outUserVisible = false;
        })
        .catch(() => {});
    }
  }
};
</script>
